package com.tinyideatech.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinyideatech.common.json.JSONObject;
import com.tinyideatech.common.utils.DateUtils;
import com.tinyideatech.system.entity.CarCode;
import com.tinyideatech.system.entity.CarDevice;
import com.tinyideatech.system.entity.CarOrder;
import com.tinyideatech.system.mapper.CarCodeMapper;
import com.tinyideatech.system.mapper.CarDeviceMapper;
import com.tinyideatech.system.mapper.CarOrderMapper;
import com.tinyideatech.system.service.ICarOrderService;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangy-5
 * @date 2019-09-09
 */
@Service
public class CarOrderServiceImpl extends ServiceImpl<CarOrderMapper, CarOrder> implements ICarOrderService {

    @Autowired
    private CarOrderMapper carOrderMapper;

    @Autowired
    private CarCodeMapper codeMapper;

    @Autowired
    private CarDeviceMapper deviceMapper;

    @Override
    public void changeStatus(CarOrder carOrder) {

    }

    @Override
    public boolean verifyOrder(String prepayId) throws NullPointerException {
        CarOrder result = carOrderMapper.selectOne(new QueryWrapper<CarOrder>().eq("prepay_id", prepayId));
        return !(result.getOpenid() == null || result.getOpenid().equals(""));
    }

    @Override
    public JSONObject compareQRcodeTime(String deviceId, Integer feature) {
        List<CarOrder> carOrders = carOrderMapper.selectList(
                new QueryWrapper<CarOrder>().eq("device_id", deviceId)
                        .eq("feature", feature)
                        .isNull("transaction_id"));
        JSONObject result = new JSONObject();
        if (carOrders == null) {
            return result;
        }
        for (CarOrder order : carOrders) {
            if (DateUtils.compareDate(order.getTimeEnd())) {
                result.put("url", order.getQrcodeUrl());
                result.put("prepayId", order.getPrepayId());
                return result;
            }
            carOrderMapper.deleteById(order);
        }
        return result;
    }

    @Override
    public boolean verifyStatus(String vehicleId, String code, Integer f) throws TooManyResultsException {

        CarDevice device = deviceMapper.selectAllById(vehicleId);
        CarCode carCode = codeMapper.selectAllByCode(code);
        if (device == null || carCode == null) {
            return false;
        } else if (carCode.getFeatures().toString().contains(f.toString())) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public XSSFWorkbook downloadRecorder() {
        List<CarOrder> carOrders = carOrderMapper.selectList(null);
        XSSFWorkbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("car_order");
        Row titleRow = sheet.createRow(0);
        titleRow.createCell(1).setCellValue("设备序号");
        titleRow.createCell(2).setCellValue("特征值");
        titleRow.createCell(3).setCellValue("回调编号");
        titleRow.createCell(4).setCellValue("订单描述");
        titleRow.createCell(5).setCellValue("openId");
        titleRow.createCell(6).setCellValue("总价格");
        int cell = 1;
        for (CarOrder carOrder : carOrders) {
            Row row = sheet.createRow(cell);
            row.createCell(0).setCellValue(cell);
            row.createCell(1).setCellValue(carOrder.getDeviceId());
            row.createCell(2).setCellValue(carOrder.getFeature());
            row.createCell(3).setCellValue(carOrder.getTransactionId());
            row.createCell(4).setCellValue(carOrder.getBody());
            row.createCell(5).setCellValue(carOrder.getOpenid());
            row.createCell(6).setCellValue(carOrder.getTotalFee());
            cell++;
        }
        return wb;
    }

}
